//
//  JTPayTool.m
//  JiongTu
//
//  Created by 杨璟 on 16/5/3.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "JTPayTool.h"

#import "JTPassengerTrip.h"

#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>

@implementation JTPayTool

+ (void)payTest {
    /*
     *点击获取prodcut实例并初始化订单信息
     */
//    Product *product = [self.productList objectAtIndex:indexPath.row];
    
    /*
     *商户的唯一的parnter和seller。
     *签约后，支付宝会为每个商户分配一个唯一的 parnter 和 seller。
     */
    
    /*============================================================================*/
    /*=======================需要填写商户app申请的===================================*/
    /*============================================================================*/
    NSString *partner = @"2088221648422012";
    NSString *seller = @"137478135@qq.com";
    NSString *privateKey = @"MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOh4kAl30iYNxP7+ty/nGAuWGsQywuqt/uVBYn385i1H3tp+JvbEGp+VlbhGk6o8l8xq82eIt544K5urk+0QE6J3BAtv0lkaVTEM6ktAHk18BcWGf3ofiLmvdXq9NItxPj5TbBb6aMW13miAPOZYXmdf+PcpkQ3BgESs1Q2uoNXbAgMBAAECgYEAlZED8uOBdHRFe138FDjVkpbHr9WM7Jp3tZ5EtRRiL13fNqoVfYycKHKAOmhGFzAW5jElW0ZuBMDU5boVWV+PkSyfyy7RDMpup/XvoKeGP5bhxPVdx+BrHyH9rB+hGRpIfhXuYFTi3TQhWvcr1HI8obB9OLn56xFirnVgQsZLTYkCQQD16GdXSLYoggo7J2ptJi8hqOMbkT8eQJzpXIMBq2v0sJ+huqdntX8bmBRepFce5IbvU/Ey4dBl4xtBuIgRP72dAkEA8gL8fEbm9yvF9X5tAvCYbfcUlbFkywvHFnD9bofpDFUIR23MiudIYa+eN5gD7zLBx15shTAtbLl/djSeTDLD1wJBAL6nJ/ABPNGdOMdyIM5tK1+CbEoES5Zt6/n1stCJklKM2cbvSzlZ+yluuGREMBOQhJlrUHbtosBL2ucSZdRRwfUCQQCVUPhc6RYJvG04CVfS74Tsm/yMW1XX3R1poC8NKjfEjxY7k+gWb5p+Au+4V0deJs/eZUjJaC8unMiistZUJyIjAkAVcWLFUhTSb1yFy1tTh9qW4o4O8TkAOsIsu9O2eJVL53QE4vjN33Eg7eU5Kf9d1AxYWkVliS8jCDqx+LC6wQfX";
    /*============================================================================*/
    /*============================================================================*/
    /*============================================================================*/
    
    //partner和seller获取失败,提示
    if ([partner length] == 0 ||
        [seller length] == 0 ||
        [privateKey length] == 0)
    {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
//                                                        message:@"缺少partner或者seller或者私钥。"
//                                                       delegate:self
//                                              cancelButtonTitle:@"确定"
//                                              otherButtonTitles:nil];
//        [alert show];
//        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSLog(@"ID有误");
        return;
    }
    
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.sellerID = seller;
    order.outTradeNO = @"123123"; //订单ID（由商家自行制定）
    order.subject = @"测试商品"; //商品标题
    order.body = @"这个商品是用来测试的"; //商品描述
    order.totalFee = @"0.01"; //商品价格
    order.notifyURL =  @"http://www.xxx.com"; //回调URL
    
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showURL = @"m.alipay.com";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
    NSString *appScheme = @"jiongtutrip";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            NSLog(@"reslut = %@",resultDic);
        }];
    }

}

+ (void)pay:(JTPassengerTrip *)trip withBlock:(payCompletionBlock)completion {
    NSString *partner = @"2088221648422012";
    NSString *seller = @"137478135@qq.com";
    NSString *privateKey = @"MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOh4kAl30iYNxP7+ty/nGAuWGsQywuqt/uVBYn385i1H3tp+JvbEGp+VlbhGk6o8l8xq82eIt544K5urk+0QE6J3BAtv0lkaVTEM6ktAHk18BcWGf3ofiLmvdXq9NItxPj5TbBb6aMW13miAPOZYXmdf+PcpkQ3BgESs1Q2uoNXbAgMBAAECgYEAlZED8uOBdHRFe138FDjVkpbHr9WM7Jp3tZ5EtRRiL13fNqoVfYycKHKAOmhGFzAW5jElW0ZuBMDU5boVWV+PkSyfyy7RDMpup/XvoKeGP5bhxPVdx+BrHyH9rB+hGRpIfhXuYFTi3TQhWvcr1HI8obB9OLn56xFirnVgQsZLTYkCQQD16GdXSLYoggo7J2ptJi8hqOMbkT8eQJzpXIMBq2v0sJ+huqdntX8bmBRepFce5IbvU/Ey4dBl4xtBuIgRP72dAkEA8gL8fEbm9yvF9X5tAvCYbfcUlbFkywvHFnD9bofpDFUIR23MiudIYa+eN5gD7zLBx15shTAtbLl/djSeTDLD1wJBAL6nJ/ABPNGdOMdyIM5tK1+CbEoES5Zt6/n1stCJklKM2cbvSzlZ+yluuGREMBOQhJlrUHbtosBL2ucSZdRRwfUCQQCVUPhc6RYJvG04CVfS74Tsm/yMW1XX3R1poC8NKjfEjxY7k+gWb5p+Au+4V0deJs/eZUjJaC8unMiistZUJyIjAkAVcWLFUhTSb1yFy1tTh9qW4o4O8TkAOsIsu9O2eJVL53QE4vjN33Eg7eU5Kf9d1AxYWkVliS8jCDqx+LC6wQfX";
    
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.sellerID = seller;
    order.outTradeNO = trip.objectId; //订单ID（由商家自行制定）
    order.subject = @"囧途而行"; //商品标题
    order.body = @"您的出行费用将由囧途帮您保管，抵达目的地后，车费将支付给司机。"; //商品描述
    order.totalFee = [trip.price stringValue]; //商品价格
    order.notifyURL =  @"http://www.xxx.com"; //回调URL
    
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showURL = @"m.alipay.com";
    
    //应用注册scheme,在plist定义URL types
    NSString *appScheme = @"jiongtutrip";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:completion];
    }
}

@end
